package com.csy.user.entity.owner.service.Impl;

import cn.hutool.core.util.IdUtil;
import com.csy.user.entity.owner.domain.AjOwner;
import com.csy.user.entity.owner.domain.AjOwnerRoom;
import com.csy.commons.vo.AjOwnerVo;
import com.csy.user.entity.owner.mapper.AjOwnerMapper;
import com.csy.user.entity.owner.mapper.AjOwnerRoomMapper;
import com.csy.user.entity.owner.service.AjOwnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: sunxx
 * @Date: 2024/4/23 - 04 - 23 - 10:48
 * @Description: com.sxx.ajcommunity.owner.service.Impl
 * @version: 1.0
 */
@Service
@Slf4j
public class AjOwnerServiceImpl implements AjOwnerService {

    @Resource
    private AjOwnerMapper ajOwnerMapper;

    @Resource
    private AjOwnerRoomMapper ajOwnerRoomMapper;

    @Override
    public List<AjOwnerVo> selectOwnerList(AjOwnerVo ajOwnerVo) {
        return ajOwnerMapper.selectOwnerList(ajOwnerVo);
    }

    @Override
    public AjOwnerVo getById(Long ownerId) {
        AjOwnerVo ajOwnerVo = new AjOwnerVo();
        ajOwnerVo.setOwnerId(ownerId);
        List<AjOwnerVo> ajOwnerVos = ajOwnerMapper.selectOwnerList(ajOwnerVo);
        return ajOwnerVos.get(0);
    }

    @Override
    @Transactional
    public int add(AjOwner ajOwner) {
        Long id = IdUtil.getSnowflake(1, 1).nextId();
        ajOwner.setOwnerId(id);
        int insert = ajOwnerMapper.insert(ajOwner);
        AjOwnerRoom ajOwnerRoom = new AjOwnerRoom();
        ajOwnerRoom.setOwnerRoomId(IdUtil.getSnowflake(1,1).nextId());
        ajOwnerRoom.setOwnerId(id);
        ajOwnerRoom.setOwnerType(ajOwner.getOwnerType());
        ajOwnerRoom.setRoomStatus("3");
        ajOwnerRoomMapper.addOwnerRoom(ajOwnerRoom);
        return insert;
    }

    @Override
    public int update(AjOwner ajOwner) {
        int i = ajOwnerMapper.updateById(ajOwner);
        return i;
    }

    @Override
    public AjOwnerVo getByName(String ownerRealName) {
        AjOwnerVo ownerVo = new AjOwnerVo();
        ownerVo.setOwnerRealName(ownerRealName);
        AjOwnerVo owner = null;
        try {
             owner = ajOwnerMapper.selectOwnerList(ownerVo).get(0);
        }catch (Exception e){
            log.error("未查询到该业主",e);
        }
        return owner;
    }

    @Override
    public int countOwner() {

        return ajOwnerMapper.countOwner();
    }
}
